﻿using System;
using System.Windows.Forms;
using EzPos.Model;
using EzPos.Model.Table;
using EzPos.Properties;
using EzPos.Service;
using EzPos.Service.Common;
using EzPos.Service.Table;

namespace EzPos.GUIs.Forms
{
    public partial class FrmTable : Form
    {
        private CommonService _commonService;
        private bool _isModified;
        private Table _table;

        private TableService _tableService;

        public FrmTable()
        {
            InitializeComponent();
        }

        public Table Table
        {
            get { return _table; }
            set { _table = value; }
        }

        private void SetModifydStatus(bool modifyStatus)
        {
            _isModified = modifyStatus;
            btnSave.Enabled = modifyStatus;
        }

        private void ModificationHandler(object sender, EventArgs e)
        {
            SetModifydStatus(true);
        }

        private void FrmUser_Load(object sender, EventArgs e)
        {
            if (_commonService == null)
                _commonService = ServiceFactory.GenerateServiceInstance().GenerateCommonService();

            SetTableInfo();
        }

        private void SetTableInfo()
        {
            if (_table == null)
                return;

            txtTableName.Text = _table.TableName;
        }

        private void BtnSaveClick(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(txtTableName.Text)) 
                {
                    const string briefMsg = "អំពីពត៌មាន";
                    var detailMsg = Resources.MsgInvalidData;
                    using (var frmMessageBox = new FrmExtendedMessageBox())
                    {
                        frmMessageBox.BriefMsgStr = briefMsg;
                        frmMessageBox.DetailMsgStr = detailMsg;
                        frmMessageBox.IsCanceledOnly = true;
                        frmMessageBox.ShowDialog(this);
                        return;
                    }
                }

                if (_table == null)
                    _table = new Table();
                _table.TableName = txtTableName.Text;

                if (_tableService == null)
                    _tableService = ServiceFactory.GenerateServiceInstance().GenerateTableService();

                _tableService.TableManagement(
                    _table,
                    _table.TableId == 0 ? 
                    Resources.OperationRequestInsert : 
                    Resources.OperationRequestUpdate);

                DialogResult = DialogResult.OK;
            }
            catch (Exception exception)
            {
                FrmExtendedMessageBox.UnknownErrorMessage(
                    Resources.MsgCaptionUnknownError,
                    exception.Message);
            }
        }

        private void TxtTableNameEnter(object sender, EventArgs e)
        {
            txtTableName.TextChanged += ModificationHandler;
        }

        private void TxtTableNameLeave(object sender, EventArgs e)
        {
            txtTableName.TextChanged -= ModificationHandler;
        }        

        private void FrmUser_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((DialogResult == DialogResult.Cancel) && (_isModified))
            {
                const string briefMsg = "អំពីការបោះបង់";
                var detailMsg = Resources.MsgOperationRequestCancel;
                using (var frmMessageBox = new FrmExtendedMessageBox())
                {
                    frmMessageBox.BriefMsgStr = briefMsg;
                    frmMessageBox.DetailMsgStr = detailMsg;
                    if (frmMessageBox.ShowDialog(this) != DialogResult.OK)
                    {
                        e.Cancel = true;
                        return;
                    }
                }
            }

            if (_isModified) 
                return;

            DialogResult = DialogResult.Cancel;
            return;
        }

        private void BtnSaveMouseEnter(object sender, EventArgs e)
        {
            btnSave.BackgroundImage = Resources.background_9;
        }

        private void BtnSaveMouseLeave(object sender, EventArgs e)
        {
            btnSave.BackgroundImage = Resources.background_2;
        }

        private void BtnCancelMouseEnter(object sender, EventArgs e)
        {
            btnCancel.BackgroundImage = Resources.background_9;
        }

        private void BtnCancelMouseLeave(object sender, EventArgs e)
        {
            btnCancel.BackgroundImage = Resources.background_2;
        }
    }
}